package com.fjw.controller;

import com.fjw.pojo.Material;
import com.fjw.pojo.Result;
import com.fjw.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/material")
public class MaterialController {

    @Autowired
    private MaterialService materialService;

    @PostMapping("/add")
    public Result add(@RequestBody Material material){
        System.out.println(material);
        materialService.add(material);
        return Result.success();
    }

    @GetMapping("/getAll")
    public Result<List<Material>> getAll(){
        List<Material> res = materialService.getAll();
        return Result.success(res);
    }

    @GetMapping("/findById")
    public Result<Material> findById(Integer id){
        System.out.println(id);
        Material res = materialService.findById(id);
        return Result.success(res);
    }

    @GetMapping("/findByAuthor")
    public Result<List<Material>> findByAuthor(Integer author){
        System.out.println(author);
        List<Material> res = materialService.findByAuthor(author);
        return Result.success(res);
    }

    @GetMapping("/findByCategory")
    public Result<List<Material>> findByCategory(String category){
        System.out.println(category);
        List<Material> res = materialService.findByCategory(category);
        return Result.success(res);
    }

    @DeleteMapping("/deleteById")
    public Result deleteById(Integer id){
        System.out.println(id);
        materialService.deleteById(id);
        return Result.success();
    }

    @PostMapping("/updateById")
    public Result updateById(@RequestBody Material material){
        System.out.println(material);
        materialService.updateById(material);
        return Result.success();
    }

}
